//
//  DKTabBar.m
//  BS1
//
//  Created by 丁侃 on 16/4/23.
//  Copyright © 2016年 丁侃. All rights reserved.
//

#import "DKTabBar.h"

@interface DKTabBar ()

@property (nonatomic, weak) UIButton *btn;

@end

@implementation DKTabBar

-(UIButton *)btn
{
    if (_btn == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateSelected];
        [self addSubview:btn];
        _btn = btn;
        
        [_btn addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _btn;
}


-(void)publishClick
{
    if (self.publichBlock) {
        self.publichBlock();
    }

}


-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat tabBarWidth = self.dk_width / 5;
    CGFloat tabBarHeight = self.dk_height;
    
    CGFloat btnWidth = tabBarWidth;
    CGFloat btnHeight = tabBarHeight;
    //遍历tabBar中的按钮,重新排布位置
    
    NSInteger index = 0;
    for (UIView *subV in self.subviews) {
        //tabBar的子控件有自带的按钮,我们添加的按钮,还有一个分割线imageView
        //所以这里需要进行判断,遍历出按钮,来重新排不位置
        //UITabBarButton是苹果的私有API,无法直接获取
        //这里需要通过NSClassFromString方式来获取私有API的类型
        if ([subV isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            subV.frame = CGRectMake(index * tabBarWidth, 0, tabBarWidth, tabBarHeight);
            if (index >= 2) {
                subV.dk_x += tabBarWidth;
            }
            index ++;
        }
    }
    
    //在layoutSubviews此方法中添加自定义按钮,此方法会被调用多次,所以不选择在这里添加
    //添加自定义按钮
    
    self.btn.frame = CGRectMake(2 * tabBarWidth, 0, btnWidth, btnHeight);


    
}

@end
